package cn.dm.controller;

import cn.dm.mapper.UserMapper;
import cn.dm.pojo.User;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("user")
public class UserController {

    // 简单demo,业务层service略,直接调用持久层接口
    @Resource
    private UserMapper userMapper;

    @RequestMapping("all")
    public List<User> findAll() {
        return userMapper.findAll();
        /**
         * 查询所有用户
         * 访问 http://localhost:8090/user/all
         * 结果
         * [
         * {
         * "id": 1,
         * "name": "zhangsan",
         * "gender": 1,
         * "age": 30,
         * "telphone": "18362610001",
         * "registerMode": "byphone",
         * "thirdPartyId": ""
         * },
         * {
         * "id": 15,
         * "name": "lisi",
         * "gender": 1,
         * "age": 20,
         * "telphone": "18362610002",
         * "registerMode": "byphone",
         * "thirdPartyId": ""
         * },
         * {
         * "id": 20,
         * "name": "wangwu",
         * "gender": 1,
         * "age": 1,
         * "telphone": "18362610003",
         * "registerMode": "byphone",
         * "thirdPartyId": ""
         * },
         * {
         * "id": 21,
         * "name": "zhaoliu",
         * "gender": 1,
         * "age": 31,
         * "telphone": "18362610004",
         * "registerMode": "byphone",
         * "thirdPartyId": ""
         * },
         * {
         * "id": 22,
         * "name": "xiaohong",
         * "gender": 2,
         * "age": 20,
         * "telphone": "18362610005",
         * "registerMode": "byphone",
         * "thirdPartyId": ""
         * }
         * ]
         */
    }


    @RequestMapping("get")
    public User getUser(@RequestParam("id") Integer id) {
        return userMapper.findUserById(id);
        /**
         * 查询指定用户(法一:普通url方式)
         * 访问 http://localhost:8090/user/get?id=1
         * 结果
         * {
         * "id": 1,
         * "name": "zhangsan",
         * "gender": 1,
         * "age": 30,
         * "telphone": "18362610001",
         * "registerMode": "byphone",
         * "thirdPartyId": ""
         * }
         */
    }


    @RequestMapping("{id}")
    public User findUser(@PathVariable Integer id) {
        return userMapper.findUserById(id);
        /**
         * 查询指定用户(法二:restful格式)
         * 访问 http://localhost:8090/user/15
         * 结果
         * {
         * "id": 15,
         * "name": "lisi",
         * "gender": 1,
         * "age": 20,
         * "telphone": "18362610002",
         * "registerMode": "byphone",
         * "thirdPartyId": ""
         * }
         */
    }
}
